package com.example.demo.HashMap_put;

/**
 * @author sunny
 */
public class NodeTest {

    static class Node{
        public int value;
        public Node next;

        public Node(int value, Node next) {
            this.value = value;
            this.next = next;
        }

        public Node(int value) {
            this.value = value;
        }
    }

    public static Node getNode(){
        Node head7 = new Node(6,null);
        Node head6 = new Node(5,head7);
        Node head5 = new Node(4,head6);
        Node head4 = new Node(3,head5);
        Node head2 = new Node(2,head4);
        Node head1 = new Node(1,head2);
        return head1;
    }

    /**
     * 1->2->2->3->3->5->6
     * @param head
     * @return
     */
    public static Node deleteRepite(Node head){
        if(head == null || head.next==null){
            return head;
        }

        Node cur = head;
        Node newHead = new Node(-1);
        Node pre = newHead;
        while(cur!=null){
            if(cur.next!=null && cur.value==cur.next.value){
                while(cur.next!=null && cur.value==cur.next.value){
                    cur = cur.next;
                }
                cur = cur.next;
                newHead.next = cur;
            }else {
                newHead.next = cur;//只有第一次对pre有效
                newHead = cur;
                cur =cur.next;
            }
        }
        return pre.next;
    }

    /**
     * t
     * r
     * 1->2->2->3->3->5->6
     * 删除重复节点 返回头结点
     * @param head
     * @return
     */
    public static Node delete(Node head){
        if(head == null || head.next==null){
            return head;
        }
        Node result = null;
        Node cur = head;
        Node temp = new Node(-1);
        while(cur!=null){
            if(cur.next!=null && cur.next.value==cur.value){
                while(cur.next!=null && cur.next.value==cur.value){
                    cur = cur.next;
                }
                cur = cur.next;
                temp.next = cur;
            }else {
                if(result==null){
                    result = cur;
                }
                temp = cur;
                cur = cur.next;
            }
        }
        return result;
    }
    public static void print(Node temp){
//        Node temp = node;
        while(temp != null){
            System.out.println(temp.value);
            temp = temp.next;
        }
    }
    public static void main(String[] args) {
        Node node = getNode();
        System.out.println("删除前");
        print(node);
        System.out.println("删除后");
        Node delete = reverse1(node);
        print(delete);

    }
    /**
     * 1->2->2->3->4->5->6
     * 6->5->3->3->2->2->1
     * 删除重复节点 返回头结点 注意next指针保留 别想太复杂
     * @param
     * @return
     */
    public static Node reverse(Node head){
        if(head ==null || head.next==null){
            return head;
        }
        Node cur = head;
        Node pre = null;
        while(cur!=null){
            Node  next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        return  pre;
    }

    /**
     * 1->2->2->3->4->5->6
     * 1<-2<-2<-3<-4<-5<-6
     * 1 reverse 2
     * 2 reverse 3
     * 3 reverse 4
     * 4 reverse 5
     * 5 6
     * 删除重复节点 返回头结点 注意next指针保留 别想太复杂
     * @param
     * @return
     */
    public static Node reverse1(Node head){
        if(head ==null|| head.next==null){
            return head;
        }

        //入 head.next=6 head 5  返回 6
        Node result = reverse1(head.next); // head 5
        Node next = head.next;
        next.next = head;
//        head.next = null;

        return result;
    }

    // num=5 num(4)*5
    // mum=4 num(3)*4
    // num=3 num(2)*3
    // num=2 num(1)*2
    // num=1 1

    public static int get(int num){
        if(num == 1){
            return 1;
        }
        int i = get(num - 1)*num;
        return i;
    }
}
